package com.baiyun.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ConnDB {
	public Connection conn = null;
	public Statement stmt = null;
	public ResultSet rs = null;

	private static String dbClassName = "oracle.jdbc.driver.OracleDriver";
	private static String dbUrl = "jdbc:oracle:"
			+ "thin:@192.168.10.7:1521:orcl";
	private static String user = "student";
	private static String password = "123456";

	public static Connection getConnection() {
		Connection conn = null;
		try {
			// 加载驱动
			Class.forName(dbClassName).newInstance();
			conn = DriverManager.getConnection(dbUrl, user, password);
		} catch (Exception ee) {
			ee.printStackTrace();
		}
		if (conn == null) {
		}
		return conn;
	}

	public ResultSet executeQuery(String sql) {
		try {
			conn = getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
		} catch (SQLException ex) {
			System.err.println(ex.getMessage());
		}
		return rs;
	}

	public int executeUpdate(String sql) {
		int result = 0;
		try {
			conn = getConnection();
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_READ_ONLY);
			result = stmt.executeUpdate(sql);
		} catch (SQLException ex) {
			result = 0;
		}
		return result;
	}

	public void close() {
		try {
			if (rs != null) {
				rs.close();
			}
			if (stmt != null) {
				stmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
	}
}
